﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using YwangHRMSys.DBUtility;


namespace YwangHRMSys.DAL
    {
        public class OldContractInfoDAL
            {
                #region 开始生成tomodel提高代码的重用性

                /// 开始生成tomodel提高代码的重用性
                private OldContractInfoModel ToModel( DataRow row )
                {
                    var model = new OldContractInfoModel();
                    model.Id = (Guid) SqlHelper.FromDbValue(row["Id"]);
                    model.OldContractSigned = (String) SqlHelper.FromDbValue(row["OldContractSigned"]);
                    model.OldContractExpires = (String) SqlHelper.FromDbValue(row["OldContractExpires"]);
                    model.EmployeeId = (Guid) SqlHelper.FromDbValue(row["EmployeeId"]);
                    model.Years = (String) SqlHelper.FromDbValue(row["Years"]);
                    model.OldContractLength = (String) SqlHelper.FromDbValue(row["OldContractLength"]);
                    model.OldContractRemarks = (String) SqlHelper.FromDbValue(row["OldContractRemarks"]);
                    return model;
                } //tomodel的结束。

                #endregion

                #region listAll开始

                //查询所有OldContractInfo的数据列表
                public IEnumerable <OldContractInfoModel> ListAll()
                {
                    var list = new List <OldContractInfoModel>();
                    DataTable table = SqlHelper.ExecuteDataTable
                            (
                                    "Select Id,OldContractSigned,OldContractExpires,EmployeeId,Years,OldContractLength,OldContractRemarks FROM  OldContractInfo");
                    foreach (DataRow row in table.Rows)
                        {
                            OldContractInfoModel model = ToModel(row);
                            list.Add(model);
                        }
                    return list;
                } //listAllOldContractInfo结束

                #endregion

                #region  根据ID来查询OldContractInfo的信息

                public OldContractInfoModel GetByOldContractInfoID( Guid Id )
                {
                    DataTable table = SqlHelper.ExecuteDataTable
                            (
                                    " SELECT Id,OldContractSigned,OldContractExpires,EmployeeId,Years,OldContractLength,OldContractRemarks FROM  OldContractInfo  WHERE  Id = @Id",
                                    new SqlParameter("@Id", Id));
                    if (table.Rows.Count <= 0)
                        {
                            return null;
                        }
                    if (table.Rows.Count == 1)
                        {
                            return ToModel(table.Rows[0]);
                        }
                    throw new Exception();
                }

                #endregion

                #region  根据ID修改OldContractInfo的信息

                public void UpdateOldContractInfo( OldContractInfoModel model )
                {
                    SqlHelper.ExecuteNonQuery
                            (
                                    "  UPDATE  OldContractInfo SET  Id = @Id,OldContractSigned = @OldContractSigned,OldContractExpires = @OldContractExpires,EmployeeId = @EmployeeId,Years = @Years,OldContractLength = @OldContractLength,OldContractRemarks = @OldContractRemarks  WHERE  Id = @Id",
                                    new SqlParameter("@Id", model.Id),
                                    new SqlParameter("@OldContractSigned", model.OldContractSigned),
                                    new SqlParameter("@OldContractExpires", model.OldContractExpires),
                                    new SqlParameter("@EmployeeId", model.EmployeeId),
                                    new SqlParameter("@Years", model.Years),
                                    new SqlParameter("@OldContractLength", model.OldContractLength),
                                    new SqlParameter("@OldContractRemarks", model.OldContractRemarks));
                }

                #endregion

                //根据ID来查询OldContractInfo的信息

                //根据ID修改OldContractInfo的信息

                // 添加一条OldContractInfo 的信息
                public void InsertOldContractInfo( OldContractInfoModel model )
                {
                    SqlHelper.ExecuteNonQuery
                            (
                                    "INSERT INTO OldContractInfo  (Id,OldContractSigned,OldContractExpires,EmployeeId,Years,OldContractLength,OldContractRemarks  ) values (@Id,@OldContractSigned,@OldContractExpires,@EmployeeId,@Years,@OldContractLength,@OldContractRemarks)",
                                    new SqlParameter("@Id", model.Id),
                                    new SqlParameter("@OldContractSigned", model.OldContractSigned),
                                    new SqlParameter("@OldContractExpires", model.OldContractExpires),
                                    new SqlParameter("@EmployeeId", model.EmployeeId),
                                    new SqlParameter("@Years", model.Years),
                                    new SqlParameter("@OldContractLength", model.OldContractLength),
                                    new SqlParameter("@OldContractRemarks", model.OldContractRemarks));
                }

            }
    }